package com.ff.xyh.consumer.dictionary.controller;


import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.entity.dictionary.ActionType;
import com.ff.xyh.consumer.dictionary.service.ActionTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 事件类型表 前端控制器
 * </p>
 *
 * @author ff
 * @since 2022-10-31
 */
@Api(tags = "消息行为类型")
@RestController
@RequestMapping("/dictionary")
public class ActionTypeController {

    @Autowired
    private ActionTypeService actionTypeService;

    @ApiOperation(value = "获取所有消息行为类型")
    @GetMapping("get-action-type-all")
    public ResultObject getAllActionType() {
        List<ActionType> list = actionTypeService.list(null);
        if (list == null || list.isEmpty()){
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            Map map = new HashMap();
            map.put("items", list);
            return ResultObject.ok().data(map);
        }
    }

    @ApiOperation(value = "根据id获取消息类型")
    @GetMapping("get-action-type-id")
    public ResultObject getActionTypeById(
            @RequestParam(name = "id") Integer id) {
        ActionType actionStatus = actionTypeService.getById(id);
        if (actionStatus == null){
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            Map map = new HashMap();
            map.put("data", actionStatus);
            return ResultObject.ok().data(map);
        }
    }

    @ApiOperation(value = "根据id删除消息类型")
    @GetMapping("delete-action-type-id")
    public ResultObject deleteActionTypeById(
            @RequestParam(name = "id") Integer id) {
        ActionType actionStatus = actionTypeService.getById(id);
        if (actionStatus == null){
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            if (actionTypeService.removeById(id)) {
                Map map = new HashMap();
                map.put("data", actionStatus);
                return ResultObject.ok().data(map);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_DELETE);
            }
        }
    }

    @ApiOperation(value = "添加消息类型")
    @PostMapping("insert-action-type")
    public ResultObject insertActionType (
            @RequestBody ActionType actionStatus) {
        if (actionTypeService.getById(actionStatus.getId()) != null){
            return ResultObject.error().message(ResultMessage.OBJECT_ALREADY_EXISTS);
        } else {
            if (actionTypeService.save(actionStatus)) {
                Map map = new HashMap();
                map.put("data", actionStatus);
                return ResultObject.ok().data(map);
            } else {
                return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT_BUT_FAILED_TO_INSERT);
            }
        }
    }

    @ApiOperation(value = "修改消息行为类型")
    @PostMapping("update-action-type")
    public ResultObject updateActionType(
            @RequestBody ActionType actionStatus) {
        if (actionTypeService.getById(actionStatus.getId()) == null){
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            if (actionTypeService.updateById(actionStatus)) {
                Map map = new HashMap();
                map.put("data", actionStatus);
                return ResultObject.ok().data(map);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_UPDATE);
            }
        }
    }
}

